//
//  EPBaseTableView.swift
//  XLProject
//
//  Created by xinglian on 2021/12/18.
//

import UIKit
import TCCKit
typealias postDataBlock = (_ dict:NSDictionary)->Void
typealias refreshDataBlock = (_ refresh : Bool)->Void

class EPBaseTableView: UITableView
{
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame:frame, style:style)
        self.setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var page : Int = 1
    var api : String = ""
    var postData : postDataBlock?
    var refreshBlock : refreshDataBlock?
    
    lazy var params : [String : Any] =
    {
        let dict = Dictionary<String, Any>()
        return dict
    }()
    
    @objc var dataArray : [Any] = Array<Any>()
    {
        didSet
        {
//            self.noDataIMG.isHidden = self.dataArray.count == 0 ? false : true
//            self.noDataTip.isHidden = self.noDataIMG.isHidden
            self.reloadData()
        }
    }
    
    lazy var noDataIMG : UIImageView =
    {
        let view = UIImageView(image:UIImage(named:"No_record"))
        view.contentMode = .scaleAspectFit
        view.isHidden = true
        self.addSubview(view)
        view.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-50)
            make.size.equalTo(CGSize(width:kUIScreenWidth * 0.27, height: kUIScreenWidth * 0.27 + 30))
//            _norecordImg.center = CGPointMake(self.view.center.x, self.view.center.y -50);
        }
        return view
    }()
    
    lazy var noDataTip : UILabel =
    {
        let view = UILabel()
        view.text = "无记录"
        view.font = font(16, .semibold)
        view.isHidden = true
        view.textColor = UIColor(red: 153 / 255.0, green: 153 / 255.0, blue: 153 / 255.0, alpha: 1)
        view.textAlignment = .center
        self.addSubview(view)
        view.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.noDataIMG.snp.bottom).offset(15)
            make.size.equalTo(CGSize(width: kUIScreenWidth, height: 20))
        }
        return view
    }()
}

//MARK: -  上拉下拉加载刷新
extension EPBaseTableView
{
     func setup()
    {
        self.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(dropDownToRefresh))
        let cmp : MJRefreshAutoNormalFooter = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(pullupToLoadMore))
        cmp.setTitle("", for: .idle)
        cmp.setTitle("", for: .refreshing)
        self.mj_footer = cmp
        
    }
    
    @objc func dropDownToRefresh()
    {
        if let block = self.refreshBlock
        {
            return  block(true)
        }
        self.page = 1
        self.loadData()
    }
    
    @objc func pullupToLoadMore()
    {
        if let block = self.refreshBlock
        {
            return  block(false)
        }
        self.page += 1
        self.loadData()
    }
    
    func loadData()
    {
        self.params["p"] = self.page
        self.params["page"] = self.page
        //        MaTool.post(self.api, params: self.params) { (responseObj, status) in
        //
        //            self.didLoadData()
        //
        //            if self.page == 1
        //            {
        //                self.dataArray .removeAll()
        //            }
        //
        //            if let block = self.postData
        //            {
        //                block(responseObj as! NSDictionary)
        //            }
        //        } failure: { Error in
        //            self.didLoadData()
        //        }
    }
    
    private func didLoadData()
    {
//        self.noDataIMG.isHidden = self.dataArray.count == 0 ? false : true
//        self.noDataTip.isHidden = self.noDataIMG.isHidden
        self.mj_header?.endRefreshing()
        self.mj_footer?.endRefreshing()
    }
    func hiddenMJRefresh()
    {
        self.mj_header?.scrollView?.bounces = false
        self.mj_footer?.scrollView?.bounces = false
        //        self.mj_header?.scrollView?.contentInset = .zero
        //        self.mj_footer?.scrollView?.contentInset = .zero
        self.mj_header?.endRefreshing()
        self.mj_footer?.endRefreshing()
        self.mj_footer?.removeFromSuperview()
        self.mj_header?.removeFromSuperview()
    }
}
